package com.jp.fm.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jp.fm.data.UserAccount;

public class LoginFilter implements Filter {

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(final ServletRequest request,
			final ServletResponse response, final FilterChain chain)
			throws IOException, ServletException {

		UserAccount uAcct = (UserAccount) ((HttpServletRequest) request)
				.getSession().getAttribute("UserAccount");

		if (null != uAcct) {
			// User is logged in, just continue request.
			chain.doFilter(request, response);
		} else {
			// Not logged in, show login page. You can eventually show the error
			// page instead.
			((HttpServletResponse) response).sendRedirect("/fm/Login.jsp");
		}
	}

	@Override
	public void init(final FilterConfig arg0) throws ServletException {
	}

}
